package box.tuson.com.tusonbox;
import android.support.v7.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View.*;
import org.apache.http.impl.client.*;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import java.util.*;
import org.apache.http.*;
import org.apache.http.message.*;
import org.apache.http.client.entity.*;
import org.apache.http.util.*;
import org.apache.http.protocol.*;

public class sjhgsd extends AppCompatActivity
{
	private EditText phoneSecEditText;  
    private TextView resultView;  
    private Button queryButton;
    private Thread thread;

    //定义消息
    private static final int NUMBER_FORMAT_ERROR = 0;
    private static final int QUERY_SUCCESS_MSG = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjhgsd);
        phoneSecEditText = (EditText) findViewById(R.id.sjhgsdEditText1);  
        resultView = (TextView) findViewById(R.id.sjhgsdTextView1);  
        queryButton = (Button) findViewById(R.id.sjhgsdButton1);
        queryButton.setOnClickListener(new QueryOnClickListener());
    }

    Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
				case NUMBER_FORMAT_ERROR:
					phoneSecEditText.setText("");
					resultView.setText("您输入的号码格式有误");
					break;
				case QUERY_SUCCESS_MSG:
					resultView.setText(msg.obj.toString());
					break;

				default:
					break;
            }
        }

    };

    String phoneSec;
    class QueryOnClickListener implements OnClickListener{

        @Override
        public void onClick(View arg0) {
            //得到手机号
            phoneSec = phoneSecEditText.getText().toString().trim();
            if("".equals(phoneSec)||phoneSec.length()<7){
                //发送消息 显示查询结果的TextView清空  
                handler.sendEmptyMessage(NUMBER_FORMAT_ERROR);
                //锁定焦点
                phoneSecEditText.requestFocus();
                return;
            }
            // 查询手机号码（段）信息  
            //getRemoteInfo(phoneSec);
            thread = new Thread(new QueryThread());
            thread.start();
        }
    }

    class QueryThread implements Runnable{

        @Override
        public void run() {
            getRemoteInfo(phoneSec);
        }
    }

	/** 
     * 手机号段归属地查询 
     * @param phoneSec 手机号段 
     */  
    private void getRemoteInfo(String phoneSec) {
        // TODO Auto-generated method stub
        // 定义待请求的URL
        String requestUrl = "https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=";
        // 创建HttpClient实例
        HttpClient client = new DefaultHttpClient();
        // 根据URL创建HttpPost实例
        HttpPost post = new HttpPost(requestUrl+phoneSec);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        // 设置需要传递的参数
        params.add(new BasicNameValuePair("value", phoneSec));
		// params.add(new BasicNameValuePair("userId", ""));
        try {
            // 设置URL编码
            post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
            // 发送请求并获取反馈
            HttpResponse response = client.execute(post);

            // 判断请求是否成功处理
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                // 解析返回的内容
                String result = EntityUtils.toString(response.getEntity());
                //将查询结果经过解析后显示在TextView中
                resultView.setText(filterHtml(result));
                Message msg = new Message();
                msg.what = QUERY_SUCCESS_MSG;
                msg.obj = filterHtml(result);
                handler.sendMessage(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /** 
     * 使用正则表达式过滤HTML标记 
     *  
     * @param source 待过滤内容 
     * @return 
     */  
    private String filterHtml(String source) {  
        if(null == source){  
            return "";  
        }  
		return source.replaceAll("","").trim();

	}  

}

